﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;


namespace MediaPlayer
{
    /// <summary>
    /// Interaction logic for VideoList.xaml
    /// </summary>
    public partial class VideoList : Window
    {
        List<VideoListClass> videoListClass = new List<VideoListClass>();
        string currentlySelectedVideo;
        MediaElement mediaElement;

        public VideoList(List<VideoListClass> videoList, ref string currentlySelectedVideo, MediaElement mediaElement)
        {
            InitializeComponent();
           
            this.mediaElement = mediaElement;
            this.currentlySelectedVideo = currentlySelectedVideo;
            videoListClass = videoList;
            foreach (var listItem in videoListClass)
            {
                VideoListBox.Items.Add(listItem.FileName);
            }
           
        }

        private void DeleteFile_Click(object sender, RoutedEventArgs e)
        {
            VideoListClass result = videoListClass.Find(vl => vl.FileName == (string)VideoListBox.SelectedItem);
            videoListClass.Remove(result);
            VideoListBox.Items.Remove(VideoListBox.SelectedItem);
        }

        private void AddFile_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Audio-Video Files|*.mp3;*.wma;*.avi;*.mpeg;*.wmv|" +
                         "All Files|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                VideoListClass vl = new VideoListClass(ofd.FileName);
                videoListClass.Add(vl);
                VideoListBox.Items.Add(vl.FileName);
            }
        }

        private void VideoListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            VideoListClass result = videoListClass.Find(vl => vl.FileName == (string)VideoListBox.SelectedItem);
            if (VideoListBox.SelectedItem != null)
            {
                currentlySelectedVideo = result.FileUrl;
            }
        }

        private void VideoListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            UIElement elem = (UIElement)VideoListBox.InputHitTest(e.GetPosition(VideoListBox));
            while (elem != VideoListBox)
            {
                if (elem is ListBoxItem)
                {
                    object selectedItem = ((ListBoxItem)elem).Content;                    
                    VideoListClass result = videoListClass.Find(vl => vl.FileName == (string)VideoListBox.SelectedItem);
                    currentlySelectedVideo = result.FileUrl;
                    mediaElement.Source = new Uri(currentlySelectedVideo);
                    mediaElement.Play();
                    return;
                }
                elem = (UIElement)VisualTreeHelper.GetParent(elem);
            }
        }

        private void UpList_Click(object sender, RoutedEventArgs e)
        {
            if (VideoListBox.SelectedIndex != -1 && VideoListBox.SelectedIndex != 0)
            {
                VideoListBox.SelectedIndex--; 
            }
        }

        private void DownList_Click(object sender, RoutedEventArgs e)
        {
            if (VideoListBox.SelectedIndex != -1)
            {
                VideoListBox.SelectedIndex++;
            }
        }

        private void DeleteAllFile_Click(object sender, RoutedEventArgs e)
        {
            videoListClass.Clear();
            VideoListBox.Items.Clear();
        }

        private void PlayFile_Click(object sender, RoutedEventArgs e)
        {
            if (VideoListBox.SelectedIndex != -1)
            {
                mediaElement.Source = new Uri(currentlySelectedVideo);
                mediaElement.Play();
            }
        }
    }
}
